package com.book.settings.controller;

import com.book.common.Result;
import com.book.settings.pojo.Admin;
import com.book.settings.service.AdminService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;

@RestController
public class AdminController {

    @Autowired
    private AdminService adminService;

    @GetMapping("/settings/admin/system/searchbyid.do")
    public Admin searchAdminById(String id){
        Admin admin = adminService.searchAdminById(id);
        return admin;
    }

    @PostMapping("/settings/admin/system/add.do")
    public Result addAdmin(@RequestBody Admin admin) throws IOException {
        int addAdminResult = adminService.addAdmin(admin);
        if(addAdminResult==1){
            return Result.success();
        }else if(addAdminResult==-1){
            return Result.error("添加的管理员已经存在");
        }else if(addAdminResult==-2){
            return Result.error("注册码错误");
        }else{
            return Result.error("系统错误");
        }
    }

    @PostMapping("/settings/admin/system/update.do")
    public Result updateAdmin(@RequestBody Admin admin){
        int updateAdminById = adminService.updateAdminById(admin);
        if(updateAdminById==1){
            return Result.success();
        }else{
            return Result.error("未找到要修改的用户");
        }
    }

}
